<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }

  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }

  .rj-cell.max span {
    font-size: 16px;
    padding-right: 20px;
  }

  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }

  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }

  .rj-cell.note label {
    float: left;
  }

  .rj-cell.note span {
    width: 80%;
    display: inline-block;
  }

  .weui-cells {
    margin-top: 0;
  }

  .weui-cells_form {
    background: #fbfbfb;
  }

  .weui-cell {
    background: #fff;
  }

  .weui-cells__title {
    text-align: left;
  }

  .oitem {
    padding: 3px;
  }

  .hide {
    display: none;
  }

  #acts {
    font-size: 0;
  }

  #acts span {
    font-size: 14px;
  }

  .halfbtn {
    display: inline-block;
    width: 49%;
  }

  .weui-label {
    text-align: left;
  }

  .time {
    border: none;
    font-size: 17px;
  }

  .weui-cell_select {
    padding: 10px 15px;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }


  .weui_uploader_input_wrp {
    float: left;
    position: relative;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 77px;
    height: 77px;
    border: 1px solid #D9D9D9;
  }

  .weui_uploader_hd {
    padding-top: 0;
    padding-right: 0;
    padding-left: 0;
  }

  .weui_uploader_files {
    list-style: none;
  }

  .weui_uploader_input_wrp:before {
    width: 2px;
    height: 39.5px;
  }

  .weui_uploader_input_wrp:before, .weui_uploader_input_wrp:after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: #D9D9D9;
  }

  .weui_uploader_input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  }

  .weui_uploader_input_wrp:after {
    width: 39.5px;
    height: 2px;
  }

  .weui_uploader_bd {
    overflow: hidden;
  }

  .weui_cell {
    padding: 10px 15px;
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
  }

  .outstore >>> .weui-uploader_files {
    list-style: none;
    overflow-y: hidden;
  }

  .outstore >>> .weui-uploader_file {
    float: left;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 79px;
    background: no-repeat 50%;
    background-size: cover;
  }

  .weui-cell.fir {
    border-top: 0;
  }

  .help {
    text-align: right;
    padding: 5px;
    font-size: 12px;
    color: #bbb;
  }

  .weui_uploader_input_wrp.inrow {
    width: 30px;
    height: 30px;
    margin: 0;
  }

  .weui_uploader_input_wrp:before {
    height: 19.5px;
  }

  .weui_uploader_input_wrp:after {
    width: 19.5px;
  }

  .weui-uploader_file.inrow {
    margin: 0;
    width: 40px;
  }

  li.weui-uploader_file {
    display: inline-block;
    padding: 3px 5px;
  }

  ul#images {
    text-align: right;
  }

  .addPur >>> .ui.selection.dropdown {
    border: none;
  }
  .addPur >>> .ui.dropdown .menu>.item {
    font-size: 18px;
  }
  .addPur >>> .ui.search.dropdown>.text {
    font-size: 18px;
  }

  /*.addPur >>> .ui.search.dropdown>.text {*/
  /*  line-height: 20px;*/
  /*}*/
</style>
<template>
  <div class="addPur" style="overflow: hidden;">
    <div
      style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      录入销售订单
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px"/>
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form method="post" id="formid">
        <input type="hidden" name="arriProvince" v-model="arriProvince" />
        <input type="hidden" name="arriCity" v-model="arriCity" />
        <input type="hidden" name="arriDistrict"  v-model="arriDistrict" />
        <input type="hidden" name="type" :value="currCustomer.flag == 3 ? 2 : 1"/>
        <input type="hidden" name="sorgguid" v-model="currDepart.orgguid"/>
        <input type="hidden" name="porgname" v-model="currDepart.orgname"/>
        <input type="hidden" name="partitionflag" :value="1"/>
        <div class="cellgroup">
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label" ref="msgDiv" style="width: 66px;">申请人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input notnull" type="text" name="applyname" readonly="true" v-model="username"
                     style="width: 25%;">
              <input type="hidden" name="depname" ref="depname" v-model="currDepart.name"/>
              <select class="weui-select" v-model="departid" id="depart" style="width: 70%;padding-left: 5px;padding-right: 5px;">
                <option value="">请选择部门</option>
                <option selected="" v-for="(item, index) in departments" :key="item.id" :value="item.id">
                  {{item.name}}
                </option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">项目</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="projId" v-model="projId">
                <option value="">请选择项目</option>
                <option v-for="(item, index) in projList" :value="item.id" >{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell" v-if="flowid == 36">
            <div class="weui-cell__hd"><label class="weui-label">订单类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="flag" v-model="flag">
                <option v-for="(item, index) in sellformFlags" :value="item.dictValue" >{{item.dictLabel}}</option>
              </select>
            </div>
          </div>
          <input v-if="flowid != 36" type="hidden" name="flag" :value="flag" />
        </div>
        <div class="cellgroup">
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label class="weui-label">客户</label></div>
            <div class="weui-cell__bd">
              <input type="hidden" name="customerguid" v-model="currCustomer.guid"/>
              <input type="hidden" name="customername" ref="customername" v-model="currCustomer.name"/>
              <model-list-select :list="customers"
                                 v-model="customerid"
                                 option-value="guid"
                                 option-text="name"
                                 placeholder="请选择" style="border: 0;">
              </model-list-select>
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">收货城市</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="endCity" name="" placeholder="请选择收货城市" v-model="endCity">
            </div>
            <div class="weui-cell__ft" v-if="hasCustomer">
              <div class="weui-vcode-btn" @click="manageAddr"><i class="weui-icon-info-circle"></i></div>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriAddress" name="arriAddress" v-model="arriAddress" placeholder="请输入收货详细地址">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货联系人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriContact" name="arriContact" v-model="arriContact" placeholder="请输入收货联系人">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriContactNo" name="arriContactNo" v-model="arriContactNo" placeholder="请输入收货人电话">
            </div>
          </div>
        </div>
        <div class="cellgroup">
          <div class="weui-cell fir">
            <div class="weui-cell__hd">
              <label class="weui-label">发货日期</label>
            </div>
            <div class="weui-cell__bd">
              <input type="text" id='datetime-picker' class="time" name="" placeholder="请选择要求发货日期" autocomplete="off"/>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd">
              <label class="weui-label">到货日期</label>
            </div>
            <div class="weui-cell__bd">
              <input type="text" id='datetime-picker2' class="time" name="" placeholder="请选择要求到货日期"/>
            </div>
          </div>
          <div class="weui-cell weui-cell_select" v-if="flowid == 36">
            <div class="weui-cell__hd"><label class="weui-label">配送方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="deliverytype" v-model="deliverytype">
                <option value="" disabled>请选择</option>
                <option v-for="(item, index) in deliverytypes" :value="item.dictValue">{{item.dictLabel}}</option>
              </select>
            </div>
          </div>
          <input v-if="flowid != 36" type="hidden" name="deliverytype" :value="2" />
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">结算币种</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="currency" v-model="currency">
                <option value="" disabled>请选择</option>
                <option selected="" v-for="(item, index) in currencys" :key="item.dictValue" :value="item.dictValue">
                  {{item.dictLabel}}
                </option>
              </select>
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">付款方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="paytype" v-model="paytype">
                <option value="" disabled>请选择</option>
                <option selected="" v-for="(item, index) in paytypes" :key="item.dictValue" :value="item.dictValue">
                  {{item.dictLabel}}
                </option>
              </select>
            </div>
          </div>
        </div>
        <div class="weui-cell fir">
          <div class="weui-cell__hd"><label class="weui-label">附件上传</label></div>
          <div class="weui-cell__ft">
            <div class="weui_uploader_bd">
              <ul class="weui_uploader_files"></ul>
              <div class="weui_uploader_input_wrp inrow">
                <UploadFile3 class="weui_uploader_input" v-on:pushfile="setFile"></UploadFile3>
              </div>
            </div>
          </div>
          <ul class="weui-uploader_files" id="images">
            <li class="weui-uploader_file" v-for="(fil, index) in filenames" @click=delFile(fil,fileids[index])>
              {{fil}}
            </li>
          </ul>
        </div>
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
              <textarea class="weui-textarea" name="remarks" placeholder="请输备注内容" rows="2"></textarea>
            </div>
          </div>
        </div>
    <div class="cellgroup">
      <div class="weui-cell fir">
        <div class="weui-cell__hd"><label class="weui-label">货品类型</label></div>
        <div class="weui-cell__bd">
          <select class="weui-select" name="quitsrtype" v-model="quitsrtype">
            <option v-for="(item, index) in productTypes" :value="item.dictValue" >{{item.dictLabel}}</option>
          </select>
        </div>
      </div>
      <div class="weui-cell weui-cell_select">
        <div class="weui-cell__hd"><label class="weui-label">计价方式</label></div>
        <div class="weui-cell__bd">
          <select class="weui-select" name="settletype" v-model="settletype">
            <option value="" disabled>请选择</option>
            <option selected="" v-for="(item, index) in settletypes" :key="item.dictValue" :value="item.dictValue">
              {{item.dictLabel}}
            </option>
          </select>
        </div>
      </div>
    </div>
        <SellformItem v-for="(item, index) in oitems" :key="index" :itemNo="item" :isLast="index+1 == oitems.length"
                      :products="products" :departid="departid"
                      :quitsrtype="quitsrtype" :settletype="settletype" :paytype="paytype"
                      :prodpricestra="prodpricestra" :currency="currency" :orgid="currDepart.orgId" @doAddItem="addItem(index + 1)"
                      @doDelItem="delItem" @changeData="takeItem"></SellformItem>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary subbtn" href="javascript:" @click="postForm(2)">提交</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  import 'vue-search-select/dist/VueSearchSelect.css'
  import { ModelListSelect } from 'vue-search-select'
  import SellformItem from "./SellformItem";
  import UploadFile3 from "../common/UploadFile3";
  import qs from 'qs';

  export default {
    inject: ['reload'],
    name: 'AddSellform2',
    data() {
      return {
        username: localStorage.username,
        customers: [],
        currCustomer: '',
        customerid: '',
        departments: [],
        currDepart: '',
        departid: '',
        fpricetype: 1,
        faretypes: [], //物流结算方式（公司结算、客户结算）
        faretypekey: 1,
        // farepricetypes: [],
        // farepricetype: '',
        customerguid: '',
        customername: '',
        products: [],
        oitems: [0],
        quitsrtype: 2,
        currStroom: '',
        settletypes: [],
        projList:[],
        projId:'',
        settletype: 1,
        paytypes: [], //付款方式（全额预付、上车付款、到货付款）
        paytype: '',
        flag: 1,
        deliverytypes: [],
        deliverytype: 2,
        discount: '',
        currencys: [], //结算币种
        currency: 1,
        prodpricestra: 2,  //为1获取不到价格不许提交，2.允许手动录入,
        senddate: '',
        arrivedate: '',
        filenames: [],
        fileids: [],
        artoken: '',
        packCity: '',
        productTypes: [],
        sellformFlags: [],
        model: {
          items: []
        },
        flowid: 34,
        arriProvince: '',
        arriCity: '',
        arriDistrict: '',
        arriAddress: '',
        arriContact: '',
        arriContactNo: '',
      }
    },
    components: {ModelListSelect, SellformItem, UploadFile3},
    computed: {
      hasCustomer: function () {
        return this.currCustomer.name ? true : false;
      },
      endCity: function() {
        if (this.arriProvince && this.arriCity && this.arriDistrict) {
          return this.arriProvince + ' ' + this.arriCity + ' ' + this.arriDistrict;
        } else if (this.arriProvince && this.arriCity) {
          return this.arriProvince + ' ' + this.arriCity;
        } else if (this.arriProvince) {
          return this.arriProvince;
        } else {
          return '';
        }
      },
    },
    created() {
      if (this.$route.query.flowid) this.flowid = this.$route.query.flowid;
      //区分电商订单、样品订单
      switch (parseInt(this.flowid)) {
        case 34:
          this.flag = 4;
          this.paytype = 3;
          break;
        case 35:
          this.flag = 1;
          this.paytype = 5;
          break;
        case 36:
          this.flag = 1;
          this.paytype = 1;
          break;
        default:
          this.flag = 1;
          this.paytype = 1;
          break;
      }
      console.log('this.flag:', this.flag)
      this.$forceUpdate();
    },
    async mounted() {
      console.log('mounted')
      let that = this;
      //日历初始化
      $("#datetime-picker").calendar();
      $("#datetime-picker2").calendar();

      $("#datetime-picker").change(function () {
        that.senddate = $(this).val().replace(/\//g, '-');
      })
      $("#datetime-picker2").change(function () {
        that.arrivedate = $(this).val().replace(/\//g, '-');
      })

      //地址初始化
      $("#endCity").cityPicker();
      $("#endCity").change(function () {
        console.log('#endCity chanage1')
        let addr = $(this).val().split(' ');
        // $('input[name="arriProvince"]').val(addr[0]);
        // $('input[name="arriCity"]').val(addr[1]);
        // $('input[name="arriDistrict"]').val(addr[2]);
        that.arriProvince = addr[0];
        that.arriCity = addr[1];
        that.arriDistrict = addr[2];
      })

      //获取select下拉数据
      this.getDepartment();
      this.getFaretype();
      this.getSettletypes();
      this.getDeliverytypes();
      this.getCurrencys();
      this.getPaytypes();
      this.getProducttypes();
      this.getSellformFlags();
      this.getProj();
    },
    activated() {
      console.log(33333333)
      // this.artoken = this.createToken();
      // //从state中获取数据并赋值
      // if (this.$store.state.arriAddr.address != '') {
      //   console.log(1111111)
      //   this.getStateAddr();
      // } else {
      //   console.log(2222)
      //   this.setCustomer();
      // }
    },
    watch: {
      'departid': function (data) {
        this.currDepart = this.departments.find(function (item) {
          return item.id == data;
        });
        this.getCustomer(this.currDepart.guid);
      },
      'customerid': function (data) {
        this.currCustomer = this.customers.filter(function (item) {
          return item.guid == data;
        })[0] || '';
        this.currency = this.currCustomer.currency != '' ? this.currCustomer.currency : 1;
        this.$store.commit('setCustomer', this.currCustomer);
      },
      'quitsrtype': function (val) {
        if (val == 2) {
          this.fpricetype = 1;
          this.settletype = 1;
          this.deliverytype = 2;
        } else if (val == 1 || val == 3) {
          this.fpricetype = 2;
          this.settletype = 2;
          this.deliverytype = 1;
        }
      },
      'deliverytype': function (val) {
        this.faretypekey = val == 1 ? 2 : 1;
      },
      currCustomer: function (currCustomer) {
        if (currCustomer.province) {
          this.arriProvince = currCustomer.province;
          this.arriCity = currCustomer.city;
          this.arriDistrict = currCustomer.district;
        }
        this.arriAddress = currCustomer.address;
        this.arriContact = currCustomer.contact;
        this.arriContactNo = currCustomer.contactno;
        //   $('#endCity').val(currCustomer.province + ' ' + currCustomer.city + ' ' + currCustomer.district);
        //   $('input[name="arriProvince"]').val(currCustomer.province);
        //   $('input[name="arriCity"]').val(currCustomer.city);
        //   $('input[name="arriDistrict"]').val(currCustomer.district);
        // }
        //
        // $('#arriAddress').val(currCustomer.address);
        // $('#arriContact').val(currCustomer.contact);
        // $('#arriContactNo').val(currCustomer.contactno);
      }
    },
    methods: {
      getProj:function (){
        this.axios({
          method:'get',
          url:'/prod-api/project/project/list'
        }).then((response)=>{
          if (response.data.code==200){
            this.projList=response.data.rows;
          }
        })
      },
      takeItem: function(val) {
        if (this.model.items.some(item => {
          return item.index == val.index;
        })) {
          this.model.items = this.model.items.map(item => {
            if (item.index == val.index) {
              return val;
            }
            return item;
          })
        } else {
          this.model.items.push(val);
        }
        console.log('this.model.items:', this.model.items)
      },
      addItem: function (index) {
        this.oitems.push(index);
      },
      delItem: function () {
        this.oitems.pop();
        this.model.items.pop();
        // this.model.items = this.model.items.filter(item => {
        //   if (item.index == val.index) {
        //     return val;
        //   }
        //   return item;
        // })
      },
      delFile: function (name, id) {
        let that = this;
        $.confirm('确定要删除？', function () {
          that.axios({
            method: 'post',
            url: '/prod-api/common/delete',
            params: {id: id}
          })
          that.fileids = that.fileids.filter(item => {
            return item != id;
          })
          that.filenames = that.filenames.filter(item => {
            return item != name;
          })
        })
      },
      setFile: function (file) {
        this.filenames.push(file.name);
        this.fileids.push(file.id);
      },
      manageAddr: function () {
        this.$router.push({name: 'AddressList2', query: {customerguid: this.currCustomer.guid, flowid: this.flowid}});
        return false;
      },
      setCustomer: function () {
        this.currCustomer = this.$store.state.customer;
      },
      getStateAddr: function () {
        let addr = this.$store.state.arriAddr;
        console.log('#endCity chanage2 addr:', addr)

        // $('input[name="arriProvince"]').val(addr.province);
        // $('input[name="arriCity"]').val(addr.city);
        // $('input[name="arriDistrict"]').val(addr.district);
        // this.packCity = addr.province + ' ' + addr.city + ' ' + addr.district;
        // $('#endCity').val(addr.province + ' ' + addr.city + ' ' + addr.district);
        // $('#arriAddress').val(addr.address);
        // $('#arriContact').val(addr.contact);
        // $('#arriContactNo').val(addr.contactno);

        this.arriProvince = addr.province;
        this.arriCity = addr.city;
        this.arriDistrict = addr.district;
        this.arriAddress = addr.address;
        this.arriContact = addr.contact;
        this.arriContactNo = addr.contactno;
      },
      //客户
      getCustomer: function (departid) {
        let that = this
        this.axios({
          method: 'get',
          url: '/prod-api/customer/customer/getCustomers',
          params: {departId: departid, functionType: 13}
        }).then(response => {
          if (response.data.code == 200) {
            that.customers = response.data.data;
            this.$forceUpdate();
          } else {
            this.toastText('获取客户数据失败');
          }
        });
      },
      //部门
      getDepartment: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/organization/department/findDepartment',
          params: {permission: 2}
        }).then(response => {
          if (response.data.code == 200) {
            that.departments = response.data.data;
            this.departid = response.data.data[0].id;
          } else if (response.data.msg) {
            this.toastText(response.data.msg);
          }  else {
            this.toastText('获取部门数据失败');
          }
        });
      },
      getSellformFlags: function() {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/sellform_flag',
        }).then(response => {
          if (response.data.code == 200) {
            that.sellformFlags = response.data.data.filter(item => item.dictValue != 4);
          } else {
            this.toastText('获取订单类型数据失败');
          }
        });
      },
      getProducttypes: function() {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/product_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.productTypes = response.data.data.filter(item => item.dictValue != 4);
          } else {
            this.toastText('获取货品类型数据失败');
          }
        });
      },
      //物流结算
      getFaretype: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/wlinfo_fare_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.faretypes = response.data.data;
            this.faretypekey = response.data.data[0].dictValue;
          } else {
            this.toastText('获取物流结算方式数据失败');
          }
        });
      },
      getSettletypes: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/sellform_pricetype',
        }).then(response => {
          if (response.data.code == 200) {
            that.settletypes = response.data.data;
            this.settletype = response.data.data[0].dictValue;
          } else {
            this.toastText('获取结算方式数据失败');
          }
        });
      },
      getDeliverytypes: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/sellform_deliverytype',
        }).then(response => {
          if (response.data.code == 200) {
            that.deliverytypes = response.data.data;
          } else {
            this.toastText('获取物流配送方式数据失败');
          }
        });
      },
      getCurrencys: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/currency_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.currencys = response.data.data;
            that.currency = response.data.data[0].dictValue;
          } else {
            this.toastText('获取结算币种数据失败');
          }
        });
      },
      getPaytypes: function () {
        let that = this;
        this.axios({
          method: 'get',
          responseType: 'json',
          url: '/prod-api/system/dict/data/type/pay_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.paytypes = response.data.data.filter(item => {
              if (this.flowid == 34) {
                return item.dictValue == 3;
              } else if (this.flowid == 35) {
                return item.dictValue == 5;
              } else {
                return item.dictValue != 5;
              }
            });
          } else {
            this.toastText('获取付款方式数据失败');
          }
        });
      },
      //提交表单数据
      postForm: function () {
        let that = this;
        //表单校验
        if (!this.checkFormElm()) {
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");
        let postData = this.serialize2Obj($("#formid").serializeArray());
        this.projList.map(item=>{
          if (item.id==postData.projId){
            postData.projName=item.name;
          }
        })
        postData.senddate = this.senddate;
        postData.arrivedate = this.arrivedate;
        postData.items = this.model.items;
        postData.depguid = this.currDepart.guid;
        postData.flowtempguid = this.flowid;
        this.axios({
          method: 'post',
          url: `/prod-api/bill/sellform/save?fileIds=${this.fileids.join(',')}&deptId=${this.departid}`,
          data: postData
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

          if (response.data.code == 200) {
            this.toastSuccess("保存成功");
            this.$router.push({name: 'SellformList2', query: {flowid: this.flowid}});
          } else if (response.data.msg == "yz") {
            let result = response.data.result;
            let txt = '预收款余额不足</br>余额：' + result.prebalance + '</br>在途订单：' + result.ztbalance + '</br>可用余额：' + result.kybalance + '</br>当前订单：' + result.balance;
            this.toastText5(txt);
          } else if (response.data.msg) {
            this.toastText(response.data.msg);
          } else {
            this.toastError('系统错误');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
          that.toastError('系统错误');
        });
      },
      checkFormElm: function () {
        console.log('this.customerid:', this.customerid)
        if (!this.departid) {
          this.toastText('请选择部门！');
          return false;
        } else if (!this.customerid) {
          this.toastText('请选择客户！');
          return false;
        } else if (this.senddate == '') {
          this.toastText('要求发货日期不能为空！');
          return false;
        } else if (this.arrivedate == '') {
          this.toastText('要求到货日期不能为空！');
          return false;
        } else if (!this.paytype) {
          this.toastText('请选择付款方式！');
          return false;
        } else if (!this.checkEmy('product')) {
          this.toastText('品种不能为空！');
          return false;
        } else if (!this.checkEmy('specif')) {
          this.toastText('请选择货品规格！');
          return false;
        } else if ([1,2,3].includes(parseInt(this.paytype)) && !this.checkEmy('price')) {
          this.toastText('货品单价不能为空！');
          return false;
        } else if ([4].includes(parseInt(this.paytype)) && !this.checkEmy2('price')) {
          this.toastText('货品单价不能为空！');
          return false;
        } else if (!this.checkEmy('weight')) {
          this.toastText('请输入货品重量！');
          return false;
        }
        return true;
      },
      //判断明细产品是否重复
      checkProduct: function () {
        let parr1 = [];
        let parr2 = [];
        let items = this.model.items;
        for (let i = 0; i < items.length; i++) {
          parr1.push({[items[i].productguid]: items[i].spguid});
          parr2.push({[items[i].productguid]: items[i].spguid});
        }
        //json数组去重
        for (var i = 0; i < parr2.length; i++) {
          for (var j = i + 1; j < parr2.length;) {
            if (Object.keys(parr2[i])[0] == Object.keys(parr2[j])[0] && Object.values(parr2[i])[0] == Object.values(parr2[j])[0]) {
              parr2.splice(j, 1);//去除重复的对象；
            } else {
              j++;
            }
          }
        }
        return parr1.length == parr2.length ? true : false;
      }
    },
    beforeRouteEnter (to, from, next) {
      to.meta.keepAlive = true
      if (from.name === 'SellformList2') {
        next(vm => {
          vm.reload()
        })
      } else {
        next(vm => {
          vm.artoken = vm.createToken();
          //从state中获取数据并赋值
          if (vm.$store.state.arriAddr.address != '') {
            console.log(1111111)
            vm.getStateAddr();
          } else {
            console.log(2222)
            vm.setCustomer();
          }
        })
      }
    },
    beforeRouteLeave (to, from, next) {
      if (to.name === 'AddressList2') {
        from.meta.keepAlive = true
        console.log('建立缓冲')
        next()
      } else {
        from.meta.keepAlive = false
        console.log('解除缓冲')
        next()
      }
    }

  }
</script>
